﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using NPOI;
using NPOI.HSSF;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace RenShiKaoQin.Account
{
    public partial class FHExport_Old : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (hrdbEntities ent = new hrdbEntities())
                {
                    var result = from kqemp in ent.TKQEmployees where kqemp.FEmployeeID.Equals(Page.User.Identity.Name) select kqemp;
                    if (result.Any())
                    {
                        TKQEmployee kq = result.Single();
                        if (!kq.IsChecker)
                        {
                            Response.Write("您无权查看此页面");
                            Response.End();
                        }
                    }
                    else
                    {
                        Response.Write("您无权查看此页面");
                        Response.End();
                    }
                }
            }
            tbStart.Attributes["readonly"] = "readonly";
            tbEnd.Attributes["readonly"] = "readonly";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (tbStart.Text == "" || tbEnd.Text == "")
            {
                return;
            }
            string deptCode = null;
            using (hrdbEntities ent = new hrdbEntities())
            {
                var result = from emp in ent.TEmployees where emp.FEmployeeID.Equals(Page.User.Identity.Name) select emp;
                TEmployee employee = result.Single();
                TDept dept = employee.TDepts.OrderBy(p => p.FValidDate).Last();
                deptCode = dept.FDeptCode;
            }
            sqlFHExport.SelectParameters.Clear();
            sqlFHExport.SelectParameters.Add("StartDate", TypeCode.DateTime, tbStart.Text);
            sqlFHExport.SelectParameters.Add("EndDate", TypeCode.DateTime, tbEnd.Text);
            sqlFHExport.SelectParameters.Add("DeptCode", TypeCode.String, deptCode.Substring(0, 2));
        }
        
        protected void btnExport_Click(object sender, EventArgs e)
        {
            /*
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=FHExport.xls");
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvFHExport.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
            */
            IWorkbook wb = new HSSFWorkbook();
            ISheet sheet = wb.CreateSheet("假期汇总");
            IRow hRow = sheet.CreateRow(0);
            int i = 0;
            foreach (TableCell cell in gvFHExport.HeaderRow.Cells)
            {
                hRow.CreateCell(i).SetCellValue(cell.Text);
                i++;
            }
            foreach (GridViewRow gvRow in gvFHExport.Rows)
            {
                IRow row = sheet.CreateRow(gvRow.RowIndex + 1);
                int cIndex = 0;
                foreach (TableCell gvCell in gvRow.Cells)
                {
                    row.CreateCell(cIndex).SetCellValue(gvCell.Text);
                    cIndex++;
                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                byte[] data = ms.ToArray();
                HttpResponse response = System.Web.HttpContext.Current.Response;
                response.Clear();
                response.Charset = "UTF-8";
                response.ContentType = "application/vnd-excel";
                response.AddHeader("Content-Disposition", string.Format("attachment; filename=FHExport.xls"));
                response.BinaryWrite(data);
                response.End();
            }
        }
    }
}